package com.umeox.watch.moto.dataservice.mqtt.request;

import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.umeox.moto.common.log.Logger;
import com.umeox.moto.mqtt.request.MqttPublishRequest;
import com.umeox.watch.moto.dataservice.mqtt.Topic;

/**
 * Created by Rambo 2020/5/7
 * Describe:
 */
public class PublishRequestFactory {


    public static MqttPublishRequest createPingPublishRequest() {
        return new PingRequest();
    }

    public static MqttPublishRequest createCommonPublishRequest(String topic,Object message) {
        return new MqttPublishRequest(topic,message);
    }

    public static MqttPublishRequest createRpcPublishRequest(String topic, String method,Object message) {
        if(TextUtils.isEmpty(topic)){
            Logger.e("topic is null");
            return null;
        }
        if(!topic.startsWith(Topic.CLOUD_RPC_REPLY_TOPIC_PREFIX)){
            Logger.e("topic is not type of RPC response");
            return null;
        }
        return new RpcRequest(topic,method,message);
    }


    public static MqttPublishRequest createEventPublishRequest(String topic,String event,Object message) {
        if(TextUtils.isEmpty(topic)){
            Logger.e("topic is null");
            return null;
        }
        if(!topic.equals(Topic.EVENT_PUBLISH_TOPIC_PREFIX)){
            Logger.e("topic is not type of event response");
            return null;
        }
        return new EventRequest(topic,event,message);
    }

    public static MqttPublishRequest createAttributePublishRequest(String topic, Object message) {
        if(TextUtils.isEmpty(topic)){
            Logger.e("topic is null");
            return null;
        }
        if(!topic.equals(Topic.GET_ATTRIBUTE_TOPIC_PREFIX)){
            Logger.e("topic is not type of SharedKey");
            return null;
        }
        return new AttributeRequest(topic,message);
    }


    public static MqttPublishRequest createClientRpcPublishRequest(String topic,Object message) {
        if(TextUtils.isEmpty(topic)){
            Logger.e("topic is null");
            return null;
        }
        if(!topic.equals(Topic.CLIENT_RPC_TOPIC_PREFIX)){
            Logger.e("topic is not type of ClientRpc");
            return null;
        }
        return new ClientRpcRequest(topic,message);
    }

    public static MqttPublishRequest createClientChatRpcPublishRequest(String topic,String requestId,Object message) {
        if(TextUtils.isEmpty(topic)){
            Logger.e("topic is null");
            return null;
        }
        if(!topic.equals(Topic.CLIENT_RPC_TOPIC_PREFIX)){
            Logger.e("topic is not type of ClientRpc");
            return null;
        }
        return new ClientChatRpcRequest(topic,requestId,message);
    }

    public static MqttPublishRequest createAckPushMsgPublishRequest(String topic, JSONObject ackMsg){
        if(TextUtils.isEmpty(topic)){
            Logger.e("topic is null");
            return null;
        }
        if(!topic.startsWith(Topic.CLOUD_PUSH_REPLY_TOPIC_PREFIX)){
            Logger.e("topic is not type of ACK topic");
            return null;
        }
        return new AckPushMsgRequest(topic,ackMsg);
    }

    public static MqttPublishRequest createClientAttrPostRequest(String topic, JSONObject attrVer){
        if(TextUtils.isEmpty(topic)){
            Logger.e("topic is null");
            return null;
        }
        if(!topic.startsWith(Topic.DEVICE_ATTRIBUTE_POST_PREFIX)){
            Logger.e("topic is not type of DEVICE_ATTRIBUTE_POST_PREFIX topic");
            return null;
        }
        return new ClientAttributeRequest(topic,attrVer);
    }
}
